package com.maimao.core.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于解决异步请求Feign时传递header
 * https://blog.csdn.net/weishaoqi2/article/details/106964787/
 * @author MaoLin Wang
 * @date 2020/12/2 2:54 下午
 */
public class ThreadLocalUtil {

    /**
     * TODO 使用InheritableThreadLocal，使得共享变量可被子线程继承
     */
    private static final InheritableThreadLocal<Map<String, String>> headerMap = new InheritableThreadLocal<Map<String, String>>() {
        @Override
        protected Map<String, String> initialValue() {
            return new HashMap<>();
        }
    };

    public static Map<String, String> get() {
        return headerMap.get();
    }

    public static String get(String key) {
        return headerMap.get().get(key);
    }

    public static void set(String key, String value) {
        headerMap.get().put(key, value);
    }
}

